package com.ylw.lang;

import org.junit.Test;

/**
 * 用于测试java lang 包里面的类
 * Created by yangliwei on 2017/7/17.
 */
public class ObjectDemo1 {

    /**
     * 测试Object里面的clone方法
     */
    @Test
    public void test01() throws CloneNotSupportedException {

        D d = new D();
        d.setD(4);
        d.setDname("D");
        C c = new C();
        c.setC(3);
        c.setCname("C");
        c.setB(2);
        c.setBname("B");
        c.setA(1);
        c.setAname("A");
        c.setD(d);

        C cloneObject = (C) c.clone();
    }

    /**
     * 测试hashCode方法
     */
    @Test
    public void testHashCode() {
        Object obj1 = new Object();
        Object obj2 = new Object();
        Integer i = obj1.hashCode();
        Integer j = obj2.hashCode();

    }

<<<<<<< HEAD
    /**
     * getClass()
     * 返回该对象所表示的类
     */
    @Test
    public void testGetClass(){
        Number n = 1;
        Class<? extends Number> nClass = n.getClass();
        System.out.println(nClass.getName());
=======
//        C cloneObject  = (C) c.clone();
    }

    /**
     * 测试Wait方法
     * 进一步查看java并发编程
     * 通过同步输出2,1,2,1,2,1
     */
    @Test
    public void testWait() {
        Object lock = new Object();
        OutputThread t1 = new OutputThread(1,lock);
        OutputThread t2 = new OutputThread(2,lock);

        Thread thread1 = new Thread(t1);
        Thread thread2 = new Thread(t2);

        thread1.start();
        thread2.start();
>>>>>>> b7d4792fc4b08d338a0966811260839d4e6dbb70
    }

    public class OutputThread implements Runnable {
        private Object lock;
        private Integer num;

        public OutputThread(Integer num,Object lock) {
            this.lock = lock;
            this.num = num;
        }

        private void output() {
            try {
                while (true) {
                    //同步这个代码块
                    synchronized (lock) {
                        lock.notifyAll();
                        System.out.println("唤醒 ："+ (num==1?2:1));
                        lock.wait();
                        System.out.println("休眠： "+num);

                    }
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }

        public void run() {
            output();
        }
    }

}
